{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f8f867f2",
   "metadata": {},
   "source": [
    "* 按字面创建列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7d031d8d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T12:08:20.305067Z",
     "start_time": "2021-10-24T12:08:20.284066Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[], [32.0, 212.0, 0.0, 81.6, 100.0, 45.3], ['hello', 'world'], ['Toyota', 'RAV4', 2.2, 60807]]\n"
     ]
    }
   ],
   "source": [
    "prices = [] # 列表为空\n",
    "temps = [32.0, 212.0, 0.0, 81.6, 100.0, 45.3 ] # 对象用逗号隔开，中括号包围\n",
    "words = ['hello', 'world'] # 一个字符串对象列表\n",
    "car_details = ['Toyota', 'RAV4', 2.2, 60807] # 包含不同类型对象的列表\n",
    "\n",
    "# 列表中的列表\n",
    "everything = [prices, temps, words, car_details ]\n",
    "odds_and_ends = [[1,2,3],['a', 'b', 'c' ],['one', 'two', 'three' ]]\n",
    "\n",
    "print(everything)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0260754",
   "metadata": {},
   "source": [
    "* 使用列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77971330",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T13:23:17.345283Z",
     "start_time": "2021-10-24T13:21:19.746Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "vowels = ['a', 'e', 'i', 'o', 'u' ]\n",
    "word = \"Millliways\"\n",
    "for letter in word :\n",
    "    if vowels in word :\n",
    "        print(letter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "57fc1298",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T12:16:48.191117Z",
     "start_time": "2021-10-24T12:16:48.165115Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a', 'e', 'i', 'o', 'u' ] \n",
    "word = \"Millliways\"\n",
    "for letter in word : # 取单词中的各个字母\n",
    "    if letter in vowels : # 如果他在‘vowels’列表中\n",
    "        print(letter) #打印这些字母"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1cb0ef4",
   "metadata": {},
   "source": [
    "* 在运行时“扩展”列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "da3f4da5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T12:37:33.282332Z",
     "start_time": "2021-10-24T12:37:33.273332Z"
    }
   },
   "outputs": [],
   "source": [
    "found = []\n",
    "len(found) # \"len\"报告一个对象的大小"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "0e6f5e8c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T12:34:21.895385Z",
     "start_time": "2021-10-24T12:34:21.878384Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如\n",
    "car_details = ['Toyota', 'RAV4', 2.2, 60807]\n",
    "len(car_details)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2595ac6",
   "metadata": {},
   "source": [
    "* 点记法语法：在列表名后面加一个点号和方法调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b8b402da",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T12:37:50.030290Z",
     "start_time": "2021-10-24T12:37:50.011289Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found.append('a') # 手滑运行了三次hhh\n",
    "                  # append 增加到列表found后面\n",
    "len(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "5448b4b5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T12:38:02.901026Z",
     "start_time": "2021-10-24T12:38:02.883025Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'a', 'a']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "9870687b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T12:44:46.825129Z",
     "start_time": "2021-10-24T12:44:46.806128Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'a', 'a', 'e', 'i', 'o', 'e', 'i', 'o']"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found.append('e')\n",
    "found.append('i')\n",
    "found.append('o')\n",
    "len(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "12e41d67",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T12:45:46.297531Z",
     "start_time": "2021-10-24T12:45:46.282530Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'a', 'a', 'e', 'i', 'o', 'e', 'i', 'o']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found # append 不小心又运行了两次hhh"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ddbcdc0",
   "metadata": {},
   "source": [
    "* 用‘in’检查成员关系"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "f410288b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T12:48:19.569298Z",
     "start_time": "2021-10-24T12:48:19.561297Z"
    }
   },
   "outputs": [],
   "source": [
    "if 'u' not in found:\n",
    "    found.append('u')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "142747ca",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T12:48:29.076841Z",
     "start_time": "2021-10-24T12:48:29.063841Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'a', 'a', 'e', 'i', 'o', 'e', 'i', 'o', 'u']"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "0195e6f0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T12:48:50.136046Z",
     "start_time": "2021-10-24T12:48:50.128045Z"
    }
   },
   "outputs": [],
   "source": [
    "if 'u' not in found: # 重复试验是否多加\n",
    "    found.append('u')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "76726618",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T12:48:55.154333Z",
     "start_time": "2021-10-24T12:48:55.138332Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'a', 'a', 'e', 'i', 'o', 'e', 'i', 'o', 'u']"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found # 试验结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "c75158fa",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T13:11:42.946566Z",
     "start_time": "2021-10-24T13:11:42.933565Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a', 'e', 'i', 'o', 'u' ] \n",
    "word = \"Millliways\"\n",
    "found = []\n",
    "for letter in word : \n",
    "    if letter in vowels : \n",
    "        if letter not in found:\n",
    "            found.append(letter)\n",
    "for vowel in found: # 第一个“for”循环结束时，第二个循环开始运行，它会显示“word”中找到的元音\n",
    "    print(vowel)         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "662db89d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T13:24:04.592986Z",
     "start_time": "2021-10-24T13:23:47.543011Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入一个需要查询元音的单词：obession\n",
      "o\n",
      "e\n",
      "i\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a', 'e', 'i', 'o', 'u' ] \n",
    "word = input(\"输入一个需要查询元音的单词：\")\n",
    "found = []\n",
    "for letter in word: \n",
    "    if letter in vowels:\n",
    "        if letter not in found:\n",
    "            found.append(letter)\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21bcdbcc",
   "metadata": {},
   "source": [
    "* 从列表中删除一个对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "97d108ca",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T13:30:25.931797Z",
     "start_time": "2021-10-24T13:30:25.875794Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums = [1,2,3,4]\n",
    "nums"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad1345cc",
   "metadata": {},
   "source": [
    "* 从列表弹出对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "4c5cc9d6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T13:32:17.099156Z",
     "start_time": "2021-10-24T13:32:17.087155Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 4]"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums.remove(3) # remove 删除的非索引值，而是对象？\n",
    "nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "aa842d75",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T13:33:09.030126Z",
     "start_time": "2021-10-24T13:33:09.019125Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums.pop() # pop空白无对象默认删除最后一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "dc5bb5aa",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T13:33:25.554071Z",
     "start_time": "2021-10-24T13:33:25.546071Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "e6a818a9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T13:34:55.629223Z",
     "start_time": "2021-10-24T13:34:55.608222Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums.pop(0) # pop 0为索引值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9b7535c",
   "metadata": {},
   "source": [
    "* 用对象扩展列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "957ce36c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T13:36:39.691175Z",
     "start_time": "2021-10-24T13:36:39.685175Z"
    }
   },
   "outputs": [],
   "source": [
    "nums.extend([3,4]) # 将一个列表追加到另一个列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "78add72d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T13:36:50.472792Z",
     "start_time": "2021-10-24T13:36:50.459791Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 4]"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c18c4e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# append 与 extend的区别：append([])一个空列表，原列表末尾会出现空列表\n",
    "#                         extend([])一个空列表，原列表末尾无变化"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed49dcb7",
   "metadata": {},
   "source": [
    "* 在列表中插入一个对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "4ed76146",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T13:43:05.030215Z",
     "start_time": "2021-10-24T13:43:04.802202Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums.insert(0,1) # 括号内（0索引值，1对象），即在0位加对象1\n",
    "nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "f9cfff54",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T13:44:44.517906Z",
     "start_time": "2021-10-24T13:44:44.506905Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, '两个半', 3, 4]"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums.insert(2,\"两个半\")\n",
    "nums"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec484fd7",
   "metadata": {},
   "source": [
    "* 牛刀小试 （Don't panic!变成on tap）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "a89c46a8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T14:10:33.264489Z",
     "start_time": "2021-10-24T14:10:33.190485Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "41f60fe7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T14:25:00.866113Z",
     "start_time": "2021-10-24T14:25:00.859112Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "o\n",
      "n\n",
      "t\n",
      " \n",
      "p\n",
      "a\n",
      "n\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "item = ['o','n','t',' ','p','a']\n",
    "for letters in phrase:\n",
    "    if letters in item:\n",
    "        print(letters)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "4e19c86c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T14:14:51.789276Z",
     "start_time": "2021-10-24T14:14:51.777275Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'!'"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8248f558",
   "metadata": {},
   "source": [
    "* 使用列表方法转换和处理对象列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "c67041bb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T14:40:17.286529Z",
     "start_time": "2021-10-24T14:40:17.276528Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "用循环4次把“nic！”删除后的plist： ['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a']\n",
      "删除D和': ['o', 'n', 't', ' ', 'p', 'a']\n",
      "['o', 'n', 't', ' ', 'a', 'p']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "\n",
    "for i in range(4): \n",
    "    plist.pop()\n",
    "# line5-6 用循环4次把nic！删除\n",
    "print(\"用循环4次把“nic！”删除后的plist：\",plist)\n",
    "\n",
    "plist.pop(0)\n",
    "plist.remove(\"'\")\n",
    "# line7-8 删除D和'\n",
    "print(\"删除D和':\" , plist)\n",
    "\n",
    "plist.extend([plist.pop(),plist.pop()])\n",
    "print(plist)\n",
    "# 两个plist.pop()分别输出为a和p，然后extend在plist末端\n",
    "\n",
    "plist.insert(2,plist.pop(3))\n",
    "# 在2号位加入删除的3号位（即空格）\n",
    "print(plist)\n",
    "\n",
    "new_phrase = ''.join(plist)\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "559ce846",
   "metadata": {},
   "source": [
    "* 复制一个列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "4ea9a7c2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T14:45:41.142052Z",
     "start_time": "2021-10-24T14:45:41.094050Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 错误演示\n",
    "first = [1,2,3,4,5]\n",
    "second = first\n",
    "second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "b70b7338",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T14:46:12.141826Z",
     "start_time": "2021-10-24T14:46:12.126825Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "second.append(6)\n",
    "second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "c0c230b6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T14:47:13.204318Z",
     "start_time": "2021-10-24T14:47:13.195318Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# second里有6 但first里也有\n",
    "first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "12052f77",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T14:49:03.956653Z",
     "start_time": "2021-10-24T14:49:03.941652Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "third = second.copy()\n",
    "third"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "3bf40cdc",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T14:49:06.330789Z",
     "start_time": "2021-10-24T14:49:06.320788Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7]"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "third.append(7)\n",
    "third"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "56d3b68d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T14:49:16.733384Z",
     "start_time": "2021-10-24T14:49:16.725383Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "82e66864",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T15:04:31.167686Z",
     "start_time": "2021-10-24T15:04:31.103683Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 中括号有支持负数索引从右边-1开始\n",
    "saying = \"Don't panic!\"\n",
    "letters = list(saying)\n",
    "letters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "357b7431",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T15:04:52.239891Z",
     "start_time": "2021-10-24T15:04:52.209890Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'!'"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "edd4503f",
   "metadata": {},
   "source": [
    "* 列表切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7c92e5b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# letters[开始：结束：步长]\n",
    "# 开始 无指定默认0\n",
    "# 结束 无指定默认最大值\n",
    "# 步长 无指定默认1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "361dc5fb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T15:14:25.202663Z",
     "start_time": "2021-10-24T15:14:25.194663Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['D', \"'\", 'p', 'i']"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[0:10:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "287047bc",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T15:15:49.067460Z",
     "start_time": "2021-10-24T15:15:49.057459Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[\"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[3:] # 从3号位开始，最大值结束，步长1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "47ad9e3b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T15:16:50.415969Z",
     "start_time": "2021-10-24T15:16:50.399968Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i']"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[:10] # 取0-9号位"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "94fd1b91",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T15:17:50.573410Z",
     "start_time": "2021-10-24T15:17:50.562409Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['D', 'n', 't', 'p', 'n', 'c']"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[: : 2] #每两个字母选择一个"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5808a05",
   "metadata": {},
   "source": [
    "* 开始和结束列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "be3b837d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T15:20:19.348919Z",
     "start_time": "2021-10-24T15:20:19.273915Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['T',\n",
       " 'h',\n",
       " 'e',\n",
       " ' ',\n",
       " 'H',\n",
       " 'i',\n",
       " 't',\n",
       " 'c',\n",
       " 'h',\n",
       " 'h',\n",
       " 'i',\n",
       " 'k',\n",
       " 'e',\n",
       " 'r',\n",
       " \"'\",\n",
       " 's',\n",
       " ' ',\n",
       " 'G',\n",
       " 'u',\n",
       " 'i',\n",
       " 'd',\n",
       " 'e',\n",
       " ' ',\n",
       " 't',\n",
       " 'o',\n",
       " ' ',\n",
       " 't',\n",
       " 'h',\n",
       " 'e',\n",
       " ' ',\n",
       " 'G',\n",
       " 'a',\n",
       " 'l',\n",
       " 'a',\n",
       " 'x',\n",
       " 'y']"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "book = \"The Hitchhiker's Guide to the Galaxy\"\n",
    "booklist = list(book)\n",
    "booklist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "385e773b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T15:21:29.561935Z",
     "start_time": "2021-10-24T15:21:29.553935Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['T', 'h', 'e']"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "booklist[0:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "c1d27479",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T15:22:12.699402Z",
     "start_time": "2021-10-24T15:22:12.689402Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['G', 'a', 'l', 'a', 'x', 'y']"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "booklist[-6:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "d29dfe76",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T15:22:59.546082Z",
     "start_time": "2021-10-24T15:22:59.538081Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The'"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "''.join(booklist[0:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "bf6c7b50",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T15:23:17.972136Z",
     "start_time": "2021-10-24T15:23:17.963135Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Galaxy'"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "''.join(booklist[-6:])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28558d6b",
   "metadata": {},
   "source": [
    "* 列表中使用步长"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "da43f335",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T15:27:11.146473Z",
     "start_time": "2021-10-24T15:27:11.131472Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"yxalaG eht ot ediuG s'rekihhctiH ehT\""
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "backwards = booklist[::-1]\n",
    "''.join(backwards)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "b0f867de",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T15:28:30.762026Z",
     "start_time": "2021-10-24T15:28:30.691022Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"TeHthie' ud oteGlx\""
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "every_other = booklist[::2]\n",
    "''.join(every_other)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "8d42cd98",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T15:29:44.577248Z",
     "start_time": "2021-10-24T15:29:44.570248Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hitchhiker'"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "''.join(booklist[4:14])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "3c2dfbf7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-24T15:30:17.822150Z",
     "start_time": "2021-10-24T15:30:17.811149Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'rekihhctiH'"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "''.join(booklist[13:3:-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "35d870c5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T01:00:33.397458Z",
     "start_time": "2021-11-14T01:00:33.379457Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'t pa'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "''.join(phrase[1:3]) and ''.join(phrase[4:8])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ca8951a9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T01:04:39.009506Z",
     "start_time": "2021-11-14T01:04:38.999506Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "new_phrase = ''.join(plist[1:3])\n",
    "new_phrase = new_phrase + ''.join([plist[5],plist[4],plist[7],plist[6]])\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07acf92a",
   "metadata": {},
   "source": [
    "* 使用'\\t'制表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "33817a2c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T01:15:10.373618Z",
     "start_time": "2021-11-14T01:15:10.359618Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\t M\n",
      "\t a\n",
      "\t r\n",
      "\t v\n",
      "\t i\n",
      "\t n\n"
     ]
    }
   ],
   "source": [
    "paranoid_android = \"Marvin\"\n",
    "letters = list(paranoid_android)\n",
    "for char in letters:\n",
    "    print('\\t',char)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f8aa9ee0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T01:37:35.156536Z",
     "start_time": "2021-11-14T01:37:35.145535Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\t M\n",
      "\t a\n",
      "\t r\n",
      "\t v\n",
      "\t i\n",
      "\t n\n",
      "\n",
      "\t\t A\n",
      "\t\t n\n",
      "\t\t d\n",
      "\t\t r\n",
      "\t\t o\n",
      "\t\t i\n",
      "\t\t d\n",
      "\n",
      "\t\t\t P\n",
      "\t\t\t a\n",
      "\t\t\t r\n",
      "\t\t\t a\n",
      "\t\t\t n\n",
      "\t\t\t o\n",
      "\t\t\t i\n",
      "\t\t\t d\n"
     ]
    }
   ],
   "source": [
    "paranoid_android = \"Marvin, the Paranoid Android\"\n",
    "letters = list(paranoid_android)\n",
    "for char in letters[:6]:\n",
    "    print('\\t',char)\n",
    "print()\n",
    "for char in letters[-7:]:\n",
    "    print('\\t'*2,char)\n",
    "print()\n",
    "for char in letters[12:20]:\n",
    "    print('\\t'*3,char)# *3 会在每个要打印的对象前面插入三个制表符（即前面空两列，使它在第三列）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "2bdfad45",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T01:51:52.851593Z",
     "start_time": "2021-11-14T01:51:52.837592Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Frod Prefect', 'Male', 'Researcher', 'Betelgeuse Seven']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person1 = ['Frod Prefect', 'Male', 'Researcher', 'Betelgeuse Seven']\n",
    "person1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f9a36d23",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T01:54:15.511753Z",
     "start_time": "2021-11-14T01:54:15.494752Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Name',\n",
       " 'Frod Prefect',\n",
       " 'Gender',\n",
       " 'Male',\n",
       " 'Occupation',\n",
       " 'Researcher',\n",
       " 'Home Planet',\n",
       " 'Betelgeuse Seven']"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person2 = ['Name', 'Frod Prefect', 'Gender', 'Male', 'Occupation', 'Researcher', 'Home Planet', 'Betelgeuse Seven']\n",
    "person2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bb8893de",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
